package com.example.badyloans.util;

import com.example.badyloans.common.ReturnResult;
import com.example.badyloans.pojo.*;
import com.example.badyloans.servlet.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @author 啊桥哦
 * @Description: 定时器
 * @date 2020-07-23 10:17
 */
@Component
public class DingshiQi {
    private static final Logger log= LoggerFactory.getLogger(DingshiQi.class);
    @Resource
    private TBorrowService tBorrowService;
    @Resource
    private TBidService tBidService;
    @Resource
    private TUserWalletService tUserWalletService;
    @Resource
    private TAccountFlowService tAccountFlowService;
    @Resource
    private TRepaymentService tRepaymentService;


    /**
     * 判断是否流标
     */
    @Scheduled(fixedRate=100000)
    public void testTwo() {
        log.info("每1分钟执行一次，判断是否流标");
        TUserWallet tUserWallet=null;
        TUserWallet tUserWallet1=null;
            //先查询出投标记录为投标中的记录
        List<TBorrow> tBorrow=tBorrowService.selectliubiao();
        for (int i=0;i<tBorrow.size();i++){
            //1、如果当前系统时间超过借款标的截止时间，就会进入流标流程，当前借款状态修改为 流标
            if(tBorrow.get(i).getBidDeadline().compareTo(LocalDateTime.now())<0){
                TBorrow tBorrow1=new TBorrow();
                tBorrow1.setBorrowState(21);
                tBorrow1.setId(tBorrow.get(i).getId());
                tBorrowService.updateByPrimaryKeySelective(tBorrow1);
                //2、将该标的投资人的钱依次退回给各位投资人
                //拿到投标人的信息
                TBid tBid=tBidService.selectByPrimary1(tBorrow.get(i).getId());
                //查出投标记录表的记录信息
                List<TBid> bids=tBidService.selectByPrimary2(tBorrow.get(i).getId());
                for (int j=0;j<bids.size();j++){
                    TBid tBid2=new TBid();
                    tBid2.setId(tBorrow.get(j).getId());
                    tBid2.setBorrowState(21);
                    tBidService.updateByPrimaryKeySelective(tBid2);
                    //1.1先查询出投资人的信息
                    tUserWallet=tUserWalletService.selectByPrimaryKey(bids.get(i).getBidUserId());
                   //流标之后进行资金回滚，把投资的钱加到用户的余额里面去
                    tUserWallet.setAccountId(bids.get(j).getBidUserId());
                   tUserWallet.setAvailableAmount(tUserWallet.getAvailableAmount()+bids.get(j).getBidAmount());
                    Long money=tUserWallet.getFreezeAmount()-bids.get(j).getBidAmount();
                   tUserWallet.setFreezeAmount(money);

                   tUserWalletService.updateByPrimaryKeySelective(tUserWallet);
                   //4，并为每位投资人生成‘退款流水’
                   TAccountFlow tAccountFlow=new TAccountFlow();
                   tAccountFlow.setAccountId(bids.get(j).getBorrowId());
                   tAccountFlow.setAmount(bids.get(j).getBidAmount());
                   tAccountFlow.setFlowType(21);
                   tAccountFlow.setAvailableAmount(tUserWallet.getAvailableAmount());
                   tAccountFlow.setFreezeAmount(tUserWallet.getFreezeAmount());
                   tAccountFlow.setRemark("退款成功，退款金额："+bids.get(j).getBidAmount()*0.01+"元");
                   tAccountFlow.setCreateTime(LocalDateTime.now());
                   tAccountFlowService.insertSelective(tAccountFlow);
                }
                //3、最后将借款人本次借款的金额，以授信额度的方式，返回给用户
                tUserWallet1=tUserWalletService.selectByPrimaryKey(tBorrow.get(i).getBorrowUserId());
                tUserWallet1.setCreditLine(tUserWallet1.getCreditLine()+tBorrow.get(i).getBorrowAmount());
                tUserWallet1.setResidualCreditLine(tUserWallet1.getResidualCreditLine()+tBorrow.get(i).getBorrowAmount());
                tUserWalletService.updateByPrimaryKeySelective(tUserWallet1);
            }
        }
    }

    /**
     * 判断是否逾期
     */
    @Scheduled(fixedRate=30000)
    public void testone(){
        log.info("每30秒执行一次，判断是否逾期还款");
        //查询出在还状态的订单
        List<TRepayment> tRepaymentList=tRepaymentService.selectBypriments();
        //查询出借款人的信息
        //循环判断是否逾期了
        for (int i=0;i<tRepaymentList.size();i++){
            //判断是否逾期
            if (tRepaymentList.get(i).getRepaymentTime().compareTo(LocalDateTime.now())<0) {
                    //逾期后，把当前状态改为逾期
                    TRepayment tRepayment=new TRepayment();
                    tRepayment.setState(1);
                    tRepayment.setId(tRepaymentList.get(i).getId());
                    int updateTrepayment=tRepaymentService.updateByPrimaryKeySelective(tRepayment);
                    //当逾期过后，把借款人的信用得分减去一分
                TUserWallet tUserWallet= tUserWalletService.selectByPrimaryKey(tRepaymentList.get(i).getBorrowUserId());

                tUserWallet.setCreditScore(tUserWallet.getCreditScore()-1);
                    tUserWallet.setCreditLine(tUserWallet.getCreditLine()-100*100L);
                    tUserWallet.setResidualCreditLine(tUserWallet.getResidualCreditLine()-100*100L);
                    tUserWalletService.updateByPrimaryKeySelective(tUserWallet);
            }
        }
    }
}
